package day8.homework;

import java.util.concurrent.ThreadPoolExecutor;

//b.通过多线程模拟两人爬山，每个线程表示一个人，
// 可以设置爬山速度  每爬完100米显示信息 先到终点的是胜利者，
// 如果有人已经到达重点了 另一个人则停止爬山
public class ChooseB {
    public static void main(String[] args) {
        /*int moun = 1000;
        int show = 100;*/
        /*Thread pep1 = new Thread(() -> {
            int speed1 = 5;
            int height = 0;
            while (height <= moun) {
                height = height + speed1;
                for (int i = 1; i < 10; i++) {
                    if (height > show * i && height % show == 0) {
                        System.out.println(Thread.currentThread().getName() + "爬到了:" + height);
                        break;
                    }
                }
            }

            System.out.println(Thread.currentThread().getName() + "登顶了");


        }, "people1");*/
        /*Thread pep2 = new Thread(() -> {
            int speed2 = 6;
            int height2 = 0;
            while (height2 <= moun) {
                height2 = height2 + speed2;
                for (int i = 0; i < 10; i++) {
                    if (height2 > show * i && height2 % show == 0) {
                        System.out.println(Thread.currentThread().getName() + "爬到了:" + height2);
                        break;
                    }
                }
            }

            System.out.println(Thread.currentThread().getName() + "登顶了");
        }, "people2");*/
        //pep1.start();
        //pep2.start();

        Climbing cli = new Climbing(1000);
        ClimbThread a = new ClimbThread(cli, 300, "DemoA");
        ClimbThread b = new ClimbThread(cli, 400, "B");
        a.start();
        b.start();
    }
}

class Climbing {
    int sum;
    boolean win = false;

    public Climbing(int sum) {
        this.sum = sum;
    }
}

class ClimbThread extends Thread {
    Climbing c;
    int speed;

    public ClimbThread(Climbing c, int speed, String name) {
        super(name);
        this.speed = speed;
        this.c = c;

    }

    @Override
    public void run() {
        int sum = 0;
        String name = Thread.currentThread().getName();
        while (true) {
            try {
                Thread.sleep(speed);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            sum += 100;
            System.out.println(name + "已经爬了" + sum + "米,剩余" + (c.sum - sum) + "米");
            if (sum >= c.sum) {
                System.out.println(name + "已经到达终点比赛结束");
                c.win = true;
            }
            if (c.win) break;

        }
    }
}